﻿layui.define(['jquery', 'jqform'], function (exports) {
    var $ = layui.jquery,
        form = layui.jqform,
        box = "",
        lform = layui.form();

    form.set({
        "blur": true,
        "form": "#form1",
    }).init();

    var obj = {
        load_province: function () {
            $.post('/Main/AreaData', function (data) {
                if (data.status == 200) {
                    $.each(data.areas, function (i, r) {
                        $('#Province').append('<option value="' + r.id + '">' + r.City + '</option>');
                    });
                    if (ProvinceId > 0) {
                        $('#Province').val(ProvinceId);
                        obj.select_province(ProvinceId);
                    }
                    lform.render('select');
                    lform.on('select(Province)', function (s) {
                        $('#City').html('<option value="all">请选择市</option>');
                        $('#County').html('<option value="all">请选择区</option>');
                        obj.select_province(s.value);
                    });
                }
            });
        },
        select_province: function (id) {
            $.post('/Main/AreaData',{parentid:id}, function (data) {
                if (data.status == 200) {
                    $.each(data.areas, function (i, r) {
                        $('#City').append('<option value="' + r.id + '">' + r.City + '</option>');
                    });
                    if (CityId > 0) {
                        $('#City').val(CityId);
                        obj.select_city(CityId);
                    }
                    lform.render('select');
                    lform.on('select(City)', function (s) {
                        $('#County').html('<option value="all">请选择区</option>');
                        obj.select_city(s.value);
                    });
                }
            });
        },
        select_city: function (id) {
            $.post('/Main/AreaData', { parentid: id }, function (data) {
                if (data.status == 200) {
                    $.each(data.areas, function (i, r) {
                        $('#County').append('<option value="' + r.id + '">' + r.City + '</option>');
                    });
                    if (CountyId > 0) {
                        $('#County').val(CountyId);
                    }
                    lform.render('select');

                }
            });
        }
    };



    obj.load_province();
    exports('arealink', obj);
});